package com.uduew.sjd.view.xpop

import android.content.Context
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.RecyclerView
import com.uduew.sjd.ui.adapter.LoanProductAdapter
import com.lxj.xpopup.core.BottomPopupView
import com.uduew.sjd.R
import com.uduew.sjd.api.ProductInfoApi

class SelectedLoanDialog(context: Context, val list: ArrayList<ProductInfoApi.ThoroughBean>?) :
    BottomPopupView(context) {
    var rvLoan: RecyclerView? = null
    var tvLoanApply: AppCompatTextView? = null
    var ivClose: AppCompatImageView? = null
    override fun getImplLayoutId(): Int {
        return R.layout.layout_loan_view
    }

    override fun onCreate() {
        super.onCreate()
        rvLoan = findViewById<RecyclerView>(R.id.rv_loan)

        tvLoanApply = findViewById<AppCompatTextView>(R.id.tv_loan_apply)

        ivClose = findViewById<AppCompatImageView>(R.id.iv_close)
        val adapters = LoanProductAdapter(context)
        rvLoan?.adapter = adapters
        adapters.submitList(list)

        adapters.setOnItemClickListener { id, item, position ->
            item.isSelected = !item.isSelected;
            adapters.notifyItemChanged(position);
            val selectedItems = adapters.currentList.filter { true }
            onLoanClickListener?.onSelectedClick(selectedItems as ArrayList<ProductInfoApi.ThoroughBean>?)
        }

        tvLoanApply?.setOnClickListener {
            onLoanClickListener?.onNow(list)
            dismiss()
        }

        ivClose?.setOnClickListener {
            dismiss()
        }
    }

    private var onLoanClickListener: OnLoanClickListener? = null

    fun setOnLoanClickListener(listener: OnLoanClickListener?) {
        this.onLoanClickListener = listener
    }

    interface OnLoanClickListener {
        fun onSelectedClick(list: ArrayList<ProductInfoApi.ThoroughBean>?)
        fun onNow(list: ArrayList<ProductInfoApi.ThoroughBean>?)
    }
}